#!/bin/bash

ARGS=("$@")

if [ ${#ARGS[@]} -ne 5 ]
then
	echo "Error: incorrect number of arguments."
	echo "Usage: train.sh config_file cs_file cs_tree_format cs_file_format output_dir"
	exit
fi

script_dir=$(dirname $0)
dist=$script_dir/../bin
pkg=edu.washington.ling.syntric.proc
jar_dir=$script_dir/../jars

config_file=${ARGS[0]}
cs_file=${ARGS[1]}
cs_tree_format=${ARGS[2]}
cs_file_format=${ARGS[3]}
output_dir=${ARGS[4]}

# Set up output directory
if [ ! -d $output_dir ]; then
	if ! mkdir $output_dir ; then
		echo Error: output_dir=\'$output_dir\' was not valid
		echo Please specify a valid output directory
		exit
	fi
fi

echo 
echo %%
echo %% 25 Attaching PS empty categories to DS
echo %%
if [ -d $output_dir/25-attach-traces ]; then
	rm -rf $output_dir/25-attach-traces
fi
mkdir $output_dir/25-attach-traces
java -cp $dist:$jar_dir/* $pkg.AttachTracesToDS $cs_file $output_dir/25-attach-traces/cs-plus.txt --0t $cs_tree_format --0f $cs_file_format --1t ssf --1f sgml 2> $output_dir/25-attach-traces/errors.txt

echo 
echo %%
echo %% 30 Checking consistency between DS and PS
echo %%
if [ -d $output_dir/30-check-consist ]; then
	rm -rf $output_dir/30-check-consist
fi
mkdir $output_dir/30-check-consist
java -cp $dist:$jar_dir/* $pkg.CheckConsistency $output_dir/25-attach-traces/cs-plus.txt $output_dir/30-check-consist/cs-plus.consist.txt $output_dir/30-check-consist/cs-plus.inconst.txt --0t ssf --0f sgml --1t ssf --1f sgml --2t ssf --2f sgml 2> $output_dir/30-check-consist/errors.txt


echo 
echo %%
echo %% 40 Extracting unary conversion rules -- text version
echo %%
if [ -d $output_dir/40-extract-unary ]; then
	rm -rf $output_dir/40-extract-unary
fi
mkdir $output_dir/40-extract-unary
java -cp $dist:$jar_dir/* $pkg.ExtractUnaryRules $output_dir/30-check-consist/cs-plus.consist.txt $output_dir/40-extract-unary/rules.txt --0t ssf --0f sgml --1u text --1f text --config $config_file 2> $output_dir/40-extract-unary/errors.txt

echo
echo %%
echo %% 41 Extracting DS-PS POS mapping rules -- text version
echo %%
if [ -d $output_dir/41-extract-POS-map ]; then
	rm -rf $output_dir/41-extract-POS-map
fi
mkdir $output_dir/41-extract-POS-map
java -cp $dist:$jar_dir/* $pkg.ExtractUnaryRules $output_dir/30-check-consist/cs-plus.consist.txt $output_dir/41-extract-POS-map/rules.txt --0t ssf --0f sgml --1u text --1f text --feats ectype --config $config_file -p 2> $output_dir/41-extract-POS-map/errors.txt

echo
echo %%
echo %% 50 Extracting binary conversion rules -- text version
echo %%
if [ -d $output_dir/50-extract-binary ]; then
	rm -rf $output_dir/50-extract-binary
fi
mkdir $output_dir/50-extract-binary
java -cp $dist:$jar_dir/* $pkg.ExtractBinaryRules $output_dir/30-check-consist/cs-plus.consist.txt $output_dir/50-extract-binary/rules.txt --0t ssf --0f sgml --1b text --1f text --config $config_file 2> $output_dir/50-extract-binary/errors.txt

